<?php
    /**
 * Viewmanager
 *
 *@author S.Feil, T.Ziegler, M.El-Hindi <goatymail@googlemail.com>
 *27 May 2012
 *@copyright Copyright (c) 2012, S.Feil, T.Ziegler, M.El-Hindi
 *@package cms
 *
 */
/**
 * Class Viewmanager
 * Holds the layout of the site and inserts the contents of templates at the right position
 *
 *@package lib
 */
    class Viewmanager{
        /**
         * @var object $oInstance Viewmanager instance
         */
        private static $oInstance = null;
        
        /**
         * 
         * @var string $sTemplatePath Path from where to include templates
         */
        private $sTemplatePath;
        
        /**
         * 
         * @var string $sLayoutPath Path from where to include the layout-template
         */
        private $sLayoutPath;
        
        /**
         * 
         * @var string $sContentPath Path from where to include the content-templates
         */
        private $sContentPath;
        
        /**
         * 
         * @var string $sCSSPath Path from where to include the css-file
         */
        private $sCSSPath;
        
        /**
         * 
         * @var string $sTemplateSuffix file suffix for templates
         */
        private $sTemplateSuffix = '.php';
        
        /**
         * 
         * @var string $sLayout The layout-template that holds the sections
         */
        private $sLayout;
        
        /**
         * 
         * @var string $sCSS name of CSS that is included
         */
        private $sCSS;

        //Declaration of all sections in the layout-template 
        
        private $sTitle = '';
        private $sContent = '';
        private $sNavigation = '';
        private $sLeftNavigation ='';
        private $sLocation ='';
        
        
        /**
         * Constructor sets all paths
         */
        private function __construct(){
            $this->sTemplatePath = CMS_ROOT . 'content/template/';
            $this->sLayoutPath = $this->sTemplatePath . 'layout/';
            $this->sContentPath = $this->sTemplatePath . 'content/';
            $this->sCSSPath = './content/template/css/';
        }

        /**
         * Static method to get singelton instance
         */
        public static function getInstance(){
            if(self::$oInstance===null){
                self::$oInstance = new self;
            }
            return self::$oInstance;
        }
        
        /**
         * Setter function for layout variable
         * @param string $sLayout Name of the layout
         */
        public function _setLayout($sLayout){
            //compose full path to layout
            $this->sLayout = $this->sLayoutPath . $sLayout . '.php';
        }
        
        /**
         * Setter function for css variable
         * @param string $sCSS Name of the css
         */
        public function _setCSS($sCSS){
            $this->sCSS = $this->sCSSPath . $sCSS . '.css';
        }
        
        /**
         * Includes the layout-template to view it in the browser
         * This method is usually called after all sections have been set
         */
        public function load(){
            include_once($this->sLayout);
        }
        
        /**
         * Assigns values to section variables defined above
         * @param string $sKey Name of section
         * @param string $sValue Value to assign to section variable
         */
        public function setSection($sKey, $sValue){
            //compose name of section variable
            //section variable consists of prefix "s" and value of $sKey
            $sVariable = 's'.$sKey;
            
            //set section variable composed above
            $this->$sVariable = $sValue;
        }
        
        /**
         * Returns instance of a view
         * Viewmanager holds all views
         * @param string $sTemplateName 
         */
        public function getView($sTemplateName){
            //compose path to template
            $sFullTemplatePath = $this->sContentPath . CMS_PREFIX . $sTemplateName . $this->sTemplateSuffix;
            //return instance of a view with the given template as parameter if it exists
            if(file_exists($sFullTemplatePath)){
                return new View($sFullTemplatePath);
                
                //template does not exists, return view with default template
            } else{
                $sFullTemplatePath = $this->sContentPath . CMS_PREFIX . CMS_DEFAULT_VIEW . $this->sTemplateSuffix;
                if(file_exists($sFullTemplatePath)){
                return new View($sFullTemplatePath);
                }else {
                    echo "Default View does not exists";
                }
            }
        }
        
    }
?>